package com.school.device;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;


@Component
public class EnvironmentPostProperties implements EnvironmentPostProcessor {
 
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String[] files = new String[]{"./School-dahua-Sdk.yml"};
        //String[] files = new String[]{"D:\\project\\project002\\vue23\\sdk-dahua-push\\School-dahua-Sdk.yml"};
        List<String> profiles=new ArrayList<String>();
        String[] activeProfiles=environment.getActiveProfiles();
        if(activeProfiles!=null && activeProfiles.length>0)
        	profiles.addAll(Arrays.asList(activeProfiles));
        String[] defaultProfiles=environment.getDefaultProfiles();
        if(defaultProfiles!=null && defaultProfiles.length>0)
        	profiles.addAll(Arrays.asList(defaultProfiles));
        for (String profile : profiles) {
            for (String str : files) {
            	System.out.print(String.format(str, profile));
            	Resource resource = new FileSystemResource(new File(String.format(str, profile)));
              environment.getPropertySources().addLast(loadProfiles(resource));
            }
        } 
    }
 
    //加载单个配置文件
    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) 
            throw new IllegalArgumentException("资源" + resource + "不存在");
        if (resource.getFilename().contains(".yml")) {
            return loadYaml(resource);
        } else {
            return loadProperty(resource);
        }
    }
 

    private PropertySource<?> loadProperty(Resource resource) {
        try {
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            return new PropertiesPropertySource(resource.getFilename(), properties);
        } catch (Exception ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
 
    private PropertySource<?> loadYaml(Resource resource) {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource);
            Properties properties = factory.getObject();
            return new PropertiesPropertySource(resource.getFilename(), properties);
        } catch (Exception ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
}